
#ifndef DATAFILEREADER_H2

#define DATAFILEREADER_H2

#include <cstdlib>

#include "meusTipos.hpp"
#include "numeros.hpp"
#include "aborta.hpp"

	
class DataFileReader{
	template<class T>
	friend DataFileReader &operator >> ( DataFileReader &s, T &v );
	
public:
	DataFileReader( MTcs &, MTcs & = "#" );
	DataFileReader( const DataFileReader & );
	~DataFileReader(){ this->close(); }
	
public:
	void   commentarySymbol( MTcs &cs )       { _commentarySymbol = cs;   }
	string commentarySymbol(   void   ) const { return _commentarySymbol; }
	int    lineNumber      (   void   ) const { return _lineNumber;       }
	int    columnNumber    (   void   ) const { return _columnNumber;     }
	int    numberOfColumns (   void   ) const { return _numberOfColumns;  }
	string filename(void){return _filename;};
	ifstream::pos_type tellg(void){return _FILE.tellg();};
	//ifstream::pos_type tellp(void){return _FILE.tellp();};
	
	template<class T> T readColumn ( MTci &C ){
		if(C<1 || C>_numberOfColumns){
			string msg="A linha "+ntos(_lineNumber)+" nao tem a coluna "+ntos(C)+".";
			msg+=" Nesta linha, as colunas sao numeradas de 1 ateh ";
			msg+=ntos(_numberOfColumns);
			aborta(msg);
		}
		T v;
		stringstream ss( _lineContent[C-1] );
		ss >> v;
		return v;
	};
	
	void   close(void){  if( _FILE.is_open() ) _FILE.close(); }
	
	bool operator++();     // Pre-incremento
	
	const DataFileReader &operator += ( MTci &n ){
		for( int i=0; i<n; i++ ) ++(*this);
		return (*this);   // Permite a concatenacao
	}  
	
	void rewind(void);
	
private:
	int      _lineNumber;
	int      _columnNumber;
	int      _numberOfColumns;
	string   _commentarySymbol;
	string   _filename;
	MTvs     _lineContent;
	ifstream _FILE;
};

#endif